package com.yehuishou.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.dao.AddressDao;
import com.yehuishou.dao.FileDao;
import com.yehuishou.pojo.BidItem;
import com.yehuishou.pojo.BidOrder;
import com.yehuishou.pojo.BidingOrder;
import com.yehuishou.pojo.Bidrecord;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.Order;
import com.yehuishou.service.BidItemService;
import com.yehuishou.service.Bid_OrderService;
import com.yehuishou.service.BidingOrderService;
import com.yehuishou.service.BidrecordService;
import com.yehuishou.service.BrandService;
import com.yehuishou.service.ClassifyService;
import com.yehuishou.service.ModelService;

//竞价商管理的控制器

@Controller
public class BiddingController {
     
	
	@Autowired
	private AddressDao addressDao;
	
	
	@Autowired
	private BidItemService bidItemService;

	String rootPath = "/upload/";

	@Autowired
	FileDao fileDao;
	
	@Autowired
	private Bid_OrderService  bid_OrderService;

	Map<String, Object> map = new HashMap<>();

	@Autowired

	private BrandService brandService;

	@Autowired

	private ClassifyService classifyService;

	@Autowired

	private ModelService modelService;

	@Autowired
	private BidrecordService bidrecordService;

	@Autowired
	private BidingOrderService bidingOrderService;

	@RequestMapping("/content-4_1")
	public String bidding(HttpServletRequest req, @RequestParam(required = true, defaultValue = "1") Integer npage,
			@RequestParam(required = true, defaultValue = "10") Integer pageSize, String status, String bidname) {
		BidItem bidItem = new BidItem();
		if (bidname != null && bidname != "") {
			bidItem.setBidname(bidname);
		}
		if (status != null && status != "") {
			bidItem.setStatus(Integer.parseInt(status));

		}

		PageHelper.startPage(npage, pageSize);
		PageInfo<BidItem> pageInfo = new PageInfo<>(bidItemService.getAllbiditem(bidItem));
		req.setAttribute("pageinfo", pageInfo);
		req.setAttribute("bidItem", bidItem);
		return "content-4_1";
	}

	// 删除一条商品
	@ResponseBody
	@RequestMapping("/deletebiditem")
	public boolean deleteBidItem(Integer bid) {
		bidItemService.deleteBidItem(bid);
		return true;
	}

	// 添加一条记录
	@RequestMapping("/addbiditem_s")
	public String daddBidItem(BidItem bidItem, HttpServletRequest req) {

		bidItemService.addBidItem(bidItem);
		return "redirect:content-4_1.html";
	}

	// 编辑
	@RequestMapping("/editbiditem")
	public String editBidItem(Integer bid) {

		return null;
	}

	// 跳转到添加页面
	@RequestMapping("/toaddpage")
	public String toaddpage(HttpServletRequest req) {

		req.setAttribute("classifylist", classifyService.getAllClassify());

		List<Brand> brandlist = brandService.getAllBrand(classifyService.getAllClassify().get(0).getCid());
		// 再获取集合中第一个classify 对象的id

		req.setAttribute("brandlist", brandlist);

		req.setAttribute("modellist", modelService.getModelByid(brandlist.get(0).getPid()));

		return "biditem_add";
	}

	// 跳转到竞价记录界面
	@RequestMapping("/content-4_2")
	public String toBidRecord(HttpServletRequest req,

			@RequestParam(required = true, defaultValue = "10") int pageSize,
			@RequestParam(required = true, defaultValue = "1") int npage, String bidname, String phone, String bid

	) {

		Bidrecord bidrecord = new Bidrecord();
		if (bidname != null && bidname != "") {

			bidrecord.setBidname(bidname);

		}
		if (phone != null && phone != "") {

			bidrecord.setPhone(phone);
		}

		if (bid != null && bid != "") {
			bidrecord.setBid(Integer.parseInt(bid));
		}
		PageHelper.startPage(npage, pageSize);
		PageInfo<Bidrecord> pageinfo = new PageInfo<>(bidrecordService.getBidRecord(phone, bidname, bid));
		req.setAttribute("pageinfo", pageinfo);
		req.setAttribute("sbidrecord", bidrecord);
		return "content-4_2";
	}

	// 获取竞价商品详情
	@RequestMapping("/getBidinfo")
	public String getBiditemDetail(HttpServletRequest req, Integer bid) {
		req.setAttribute("bidinfo", bidItemService.getBiditemDetial(bid));
		return "bid_info";
	}

	// 验证添加的商品是否存在
	@ResponseBody
	@RequestMapping("/checkBiditemExist")
	public Map<String, Object> checkBiditemExist(HttpServletRequest req, BidItem biditem) {

		boolean rs = bidItemService.checkBidItemExsit(biditem);
		map.put("rs", rs);
		return map;
	}

	// 竞价商品编辑
	@RequestMapping("/tobidedit")
	public String tobiditem_edit(HttpServletRequest req, Integer bid) {

		BidItem bi = bidItemService.getBiditemDetial(bid);
		req.setAttribute("bid", bi);

		return "biditem_edit";// 竞价商品状态为竞价 就只能修改它的价格
	}

	// 修改竞价商品的信息
	@ResponseBody
	@RequestMapping("/saveBidEdit")
	public Map<String, Object> tobiditem_edit(HttpServletRequest req, String bid, String bidname, String delivery_time,
			String lowprice, String status) {
           
		System.out.println(bid);
		Map<String, Object> map = new HashMap<>();
		BidItem bi = new BidItem();
		bi.setLowprice(Double.parseDouble(lowprice));
		bi.setStatus(Integer.parseInt(status));
		bi.setDelivery_time(delivery_time);
		bi.setBidname(bidname);
		bi.setBid(Integer.parseInt(bid));
		bidItemService.updateBidItemAll(bi);
		map.put("flag", true);
		return map;
	}

	// 生成竞价订单
	@ResponseBody
	@RequestMapping("/creatOrder")
	public boolean bidingOrder(Integer id) {
		Bidrecord re = bidrecordService.getOneById(id);

		BidItem bi = new BidItem();
		bi.setLowprice(re.getBidprice());
		bi.setBid(re.getBid());

		// 修改竞价商品的价格为当前成交金额
		bidItemService.updateBidItemByid(bi);
         
		//获取对应该竞价商品的所有的竞价记录
	  List<Bidrecord> res =bidrecordService.getBidRecord(re.getBid());
		
	  //获取所有的对应该竞价商品的竞价记录的id
	  List<Integer> ids = new ArrayList<>();
	  
	  for(int i=0;i<res.size();i++) {
		  ids.add(res.get(i).getId());
	  }
	//修改所有的该竞价商品的记录的状态2
	/*用户进行竞价记录的评判 将生成订单  将竞价记录状态改为3完成*/
	  bidrecordService.updateRecord(ids, 3);
	   
		Order or = new Order();
		or.setPayment(re.getBidprice());
		or.setUid(re.getUid());
		//订单类型为4 竞价订单
		or.setOrder_type(4);
		//状态为未支付
		or.setStatus(1);
		//生成订单号
		String oid =new Date().getTime()+""+(int)((Math.random()*9+1)*10000);
		or.setOid(oid);
		//生成竞价商品的订单
		bidingOrderService.insertBidingOrder(or);
		//插入订单中间表
		BidOrder bOrder= new BidOrder();
		bOrder.setBid(re.getBid());
		bOrder.setOid(oid);
		bid_OrderService.insertBidOrder(bOrder);
		
		boolean flag = true;
		return flag;

	}

   // 竞价订单
	@RequestMapping("/content-4_3")
	public String tobidingOrder(
			HttpServletRequest req, 
			String npage, 
			String phone,
			String status,
			String createtime,
			String updatetime
			) {

		BidingOrder bo = new BidingOrder();
		
		Integer page = 1;

		if (npage != null && npage != "") {

			page = Integer.parseInt(npage);
		}
		if (phone != null && phone != "") {
			bo.setPhone(phone);
		}
		
		if(status!=null&&status!="") {
		   
			bo.setStatus(Integer.parseInt(status));
		}
		
		if(createtime!=null&&createtime!="") {
			
			bo.setCreatetime(createtime);
		}
		
        if(updatetime!=null&&updatetime!="") {
			
			bo.setUpdatetime(updatetime);
		}
		
		
		System.out.println("无敌"+bo);
		PageHelper.startPage(page, 10);
		PageInfo<BidingOrder> pageInfo = new PageInfo<>(bidingOrderService.selectBidingOrder(bo));

		req.setAttribute("pageinfo", pageInfo);
		req.setAttribute("search", bo);

		return "content-4_3";
	}

	 //删除一条订单
    @ResponseBody  
	@RequestMapping("/delelteOrder")
	public Map<String, Object> delelteOrder(String oid) {
		bidingOrderService.deleteOneOrder(oid);
		map.put("flag", true);
		return map;
	}

     //删除竞价记录
	@ResponseBody
	@RequestMapping("/deleterecord")
	public Map<String, Object> deleterecord(Integer id) {
		bidrecordService.deleteBidRecord(id);
		map.put("flag", true);
		return map;
	}
	
	//批量删除记录
	@ResponseBody
	@RequestMapping("/deletebid_redcord")
	public Map<String, Object> 	deletebid_redcord(@RequestParam(value = "td_s[]") Integer [] td_s) {
		bidrecordService.deleteBidRecords(td_s);
		map.put("flag", true);
		return map;
	}
	

	//进入订单编辑的页面
	@RequestMapping("/getOneBidOrder")
	public String getOneBidOrder(HttpServletRequest req,String oid) {
		req.setAttribute("bidorder", bidingOrderService.selectOneOrder(oid));
		return "bidOrder_edit";//进入订单编辑的页面
	}
	
	//更新订单信息
	@ResponseBody
	@RequestMapping("/saveBidOrderEdit")
	public Map<String, Object> editBidOrder(BidingOrder or) {
		Integer s =bidingOrderService.updateBidOrder(or);
		boolean flag =false;
		if(s>0) {
			flag =true;
		}
		map.put("flag", flag);
		return map;
	}
	//获取收取地址详情
	@RequestMapping("/getAddressByaid")
	public String getAddressByaid(HttpServletRequest req,Integer aid) {
		req.setAttribute("address", addressDao.getAll(aid));
	  return "address_info";
	}
	
	//批量删除竞价商品
	@ResponseBody
	@RequestMapping("/deletebiditems")
	public Map<String, Object> deletebiditems(@RequestParam(value = "td_s[]") Integer [] td_s) {
		bidItemService.deleteBidItems(td_s);
		boolean flag = true;
		map.put("flag", flag);
		return map;
	}
	
	//批量删除订单
	@ResponseBody
	@RequestMapping("/deletebidOrders")
	public Map<String, Object> deletebidOrders(@RequestParam(value = "td_s[]") Integer [] td_s) {
		
		bid_OrderService.deleteBidOrders(td_s);
		boolean flag = true;
		map.put("flag", flag);
		return map;
	}	
}
